package com.openbravo.data.loader.dialect.H2;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DBSession;
import com.openbravo.data.loader.dialect.AbstractDataBase;
import e.odbo.DB;

public class H2 extends AbstractDataBase<H2> {
    private static String JDBCDriver="org.h2.Driver";

    public static String NAME="H2";

    private static String URLPREV="jdbc:h2:";

    public static class TYPE{

    }

    public H2() {
        super(NAME);
    }


    public static DB connectLocal(String user, String password, String database)throws BasicException {
        return connect("localhost",database,user,password);
    }

    public static DB connect(String host,String database,String user,String password)throws BasicException {
        try {
            Class.forName(JDBCDriver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new BasicException(e.getMessage());
        }
        String url = String.format(URLPREV+"tcp://%s/%s", host, database);
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }

    //sample in the current(!) working directory
    public static DB embedded(String database,String user, String password)throws BasicException{
        String url = String.format(URLPREV+"%s",  database);
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }

    public static DB embeddedHomeDir(String database,String user, String password)throws BasicException{
        return embedded("~/"+database,user,password);
    }

    public static DB embeddedDir(String path,String database,String user, String password)throws BasicException{
        return embedded(path+"/"+database,user,password);
    }

    public static DB mem(String database,String user, String password)throws BasicException{
        String url = String.format(URLPREV+"mem:%s",  database);
        DBSession dbSession=new DBSession(url,user,password);
        return DB.getInstance(dbSession);
    }


}
